package com.visionet.school.app.web.common.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
//	private static String[] cookieKeys = new String[]{"loginUserId","userId","userName","loginUserName","roleName","sid","userImgUrl","lastLoginName","loginBgUrl","logoUrl","nsid","nuid","nuname","nuprofilePhoto","webBgUrl"};
	

	public static void saveCookie(String key,String value,HttpServletResponse response,HttpServletRequest request){
		Cookie[] cookies= request.getCookies();
		Cookie cookie = null;
		
		if(cookies != null){
			for(Cookie c: cookies){
				if(key.equals(c.getName())){
					cookie = c;
				}
			}
		}
		
		if(cookie == null){
			cookie = new Cookie(key, value);
		}
		
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static void clearCookie(HttpServletResponse resp,HttpServletRequest request){
		Cookie[] cookies= request.getCookies();
		
		if(cookies != null){
			for(Cookie cookie: cookies){
				if(cookie.getName() != null && cookie.getName().length() > 0 && !"lastLoginName".equals(cookie.getName())){
					cookie.setMaxAge(0);
					cookie.setPath("/");
					resp.addCookie(cookie);
				}
			}
		}
	}
	
}
